$(function () {
    getSexByCode();
    getLeftNavByRole();
})
var userCode = sessionStorage.getItem("userCode");
console.log(userCode);

// 左侧导航栏js
function ChangeMenu(obj){
    let current_header = obj;
    let item_list = current_header.parentElement.parentElement.children;
    for(let i=0;i<item_list.length;i++){
        let current_item=item_list[i];
        current_item.children[1].classList.add('hide');
    }
    current_header.nextElementSibling.classList.remove('hide');
}

function setName(data) {
    if (userCode != null) {
        $("#loginUser").html(data.name);
        let teamName = data.teamName == null ? "暂无":data.teamName;
        $("#teamName").html(teamName);
        $("#teamCode").html(data.teamCode);
    } else {
        window.location.href = "/html/login.html";
    }
}

// 获取登录用户的角色
function getLeftNavByRole() {
    let data = {userCode:userCode};
    let result = jsonGetValue("/user/findUserByUserCode", data);
    console.log(result);
    setLeftNavData(result.data.data);
}
// 根据角色显示菜单
function setLeftNavData(data) {
    $("#roleId").val(data.roleId);
    let html = " ";
    if (data.roleId == 1){
        html += " <div>\n" +
                "     <div class='header' id='permissionManage' onclick='ChangeMenu(this);'>权限管理</div>\n" +
                "     <div class='content'>\n" +
                "         <div id='permission' onclick=\"go(this,'/permission/permission-list.html')\">项目权限</div>\n" +
                "         <div id='role' onclick=\"go(this,'/role/role-list.html')\">角色权限</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='userManage' onclick='ChangeMenu(this);'>用户管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='userList' onclick=\"go(this,'/user/user-list.html')\">用户列表</div>\n" +
                "         <div id='loginInfo' onclick=\"go(this,'/login/login-info.html')\">登录记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='studentManage' onclick='ChangeMenu(this);'>学员管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='studentList' onclick=\"go(this,'/user/student-list.html')\">学员列表</div>\n" +
                "         <div id='teamList' onclick=\"go(this,'/team/team-list.html')\">小组列表</div>\n" +
                "         <div id='teamInfo' onclick=\"go(this,'/team/team-info.html')\">调组记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='signManage' onclick='ChangeMenu(this);'>签到管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='signList' onclick=\"go(this,'/sign/sign-list.html')\">签到列表</div>\n" +
                "         <div id='signInfo' onclick=\"go(this,'/sign/sign-info.html')\">签到记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='logsManage' onclick='ChangeMenu(this);'>日报管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='myLogs' onclick=\"go(this,'/logs/my-logs.html')\">我的日报</div>\n" +
                "         <div id='LogsList' onclick=\"go(this,'/logs/logs-list.html')\">学员日报</div>\n" +
                "     </div>\n" +
                " </div>";
    }
    else if (data.roleId == 3 || data.roleId == 4){
            html += " <div>\n" +
                "     <div class='header' id='userManage' onclick='ChangeMenu(this);'>用户管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='loginInfo' onclick=\"go(this,'/login/login-info.html')\">登录记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='studentManage' onclick='ChangeMenu(this);'>学员管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='studentList' onclick=\"go(this,'/user/student-list.html')\">学员列表</div>\n" +
                "         <div id='teamList' onclick=\"go(this,'/team/team-list.html')\">小组列表</div>\n" +
                "         <div id='teamInfo' onclick=\"go(this,'/team/team-info.html')\">调组记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='signManage' onclick='ChangeMenu(this);'>签到管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='signInfo' onclick=\"go(this,'/sign/sign-info.html')\">签到记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='logsManage' onclick='ChangeMenu(this);'>日报管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='LogsList' onclick=\"go(this,'/logs/logs-list.html')\">学员日报</div>\n" +
                "     </div>\n" +
                " </div>";
    }
    else if (data.roleId == 5){
        html += " <div>\n" +
                "     <div class='header' id='userManage' onclick='ChangeMenu(this);'>用户管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='loginInfo' onclick=\"go(this,'/login/login-info.html')\">登录记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='signManage' onclick='ChangeMenu(this);'>签到管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='signList' onclick=\"go(this,'/sign/sign-list.html')\">签到列表</div>\n" +
                "         <div id='signInfo' onclick=\"go(this,'/sign/sign-info.html')\">签到记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='logsManage' onclick='ChangeMenu(this);'>日报管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='myLogs' onclick=\"go(this,'/logs/my-logs.html')\">我的日报</div>\n" +
                "     </div>\n" +
                " </div>";
    }
    else {
        html += " <div>\n" +
                "     <div class='header' id='userManage' onclick='ChangeMenu(this);'>用户管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='loginInfo' onclick=\"go(this,'/login/login-info.html')\">登录记录</div>\n" +
                "     </div>\n" +
                " </div>\n" +
                " <div>\n" +
                "     <div class='header' id='logsManage' onclick='ChangeMenu(this);'>日报管理</div>\n" +
                "     <div class='content hide'>\n" +
                "         <div id='myLogs' onclick=\"go(this,'/logs/my-logs.html')\">我的日报</div>\n" +
                "     </div>\n" +
                " </div>";
    }
    html += " <div>\n" +
            "     <div class='header' id='wordManage' onclick='ChangeMenu(this);'>单词管理</div>\n" +
            "     <div class='content hide'>\n" +
            "         <div id='wordList' onclick=\"go(this,'/word/word-list.html')\">单词列表</div>\n" +
            "         <div id='myCollect' onclick=\"go(this,'/word/my-collect.html')\">我的收藏</div>\n" +
            "     </div>\n" +
            " </div>";
    $("#leftNav").html(html);
}

// 左侧导航栏页面显示
function go(obj, page) {
    $(".right").load("/html" + page);
}

// 从码表获取性别
function getSexByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"sex"});
    console.log(result);
    setSexDataByCode(result.data.data);
}
// 给性别单选按钮赋值
function setSexDataByCode(data) {
    console.log(data);
    let html = "";
    for (let i=0;i<data.length;i++){
        html += '<label><input type="radio" name="sex" value="'+ data[i].code +'">'+ data[i].key +'</label>';
    }
    $("#perfectSex").html(html);
    getEducationByCode();
}

// 从码表获取学历
function getEducationByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"education"});
    console.log(result);
    setEducationDataByCode(result.data.data);
}
// 给学历下拉列表赋值
function setEducationDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#perfectEducation").html(html);
    findUserByUserCode();
}

// 根据用户编号查询用户
function findUserByUserCode() {
    let result = jsonGetValue("/user/findUserByUserCode",{userCode: userCode});
    console.log(result);
    setUserDataByUserCode(result.data.data);
}
// 获取页面输入的值
function setUserDataByUserCode(data) {
    $("#userCode").val(data.userCode);
    $("#perfectName").val(data.name);
    $("input[name='sex'][value='"+ data.sexCode +"']").attr("checked",true);
    $("#perfectEducation").find("option[value='"+ data.education +"']").prop("selected",true);
    $("#address").val(data.address);
    $("#phone").val(data.phone);
    setName(data);
}

// 完善信息
function perfect() {
    let data = {
        userCode:userCode,
        name:$.trim($("#perfectName").val()),
        sexCode:$.trim($("input[name='sex']:checked").val()),
        education:$.trim($("#perfectEducation").val()),
        address:$.trim($("#address").val()),
        phone:$.trim($("#phone").val()),
    }
    let result = jsonPost("/user/perfect", data);
    console.log(result)
    if (result.result == 1){
        alert("信息完善成功");
        findUserByUserCode();
    }else if(result.result == -1){
        alert("修改失败");
    }
}

// 退出登录
function loginOut(){
    let result = myJson("/loginOut");
    console.log(result);
    if (result.data.result == null){
        window.location.href = "/html/login.html";
    }
}